package gjtx6;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Supermarket {
    private String SupermarketName;
    private String site;
    Member mb=new Member();
    Set<Member> set=new TreeSet<>();

    public Supermarket(String supermarketName, String site) {
        SupermarketName = supermarketName;
        this.site = site;
    }

    public String getSupermarketName() {
        return SupermarketName;
    }

    public void setSupermarketName(String supermarketName) {
        SupermarketName = supermarketName;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    @Override
    public String toString() {
        return "超市名：" + SupermarketName +
                ",地址：" + site ;
    }
//1会员开卡
    public boolean card(String number, String name, String phone, int integral, String password){
        mb=new Member(number,name,phone,integral,password);
        Iterator<Member> ir=set.iterator();
        while (ir.hasNext()){
            Member mb1=ir.next();
            if (mb1.getNumber().equals(number)) {
                System.out.println("会员号重复！");
                return false;
            }
        }
        set.add(mb);
        System.out.println("已开卡！");
        return true;
    }
    //2查询积分
    public void inquires(){
        System.out.println("会员信息如下：");
        for (Member s:set)
            System.out.println(s);
    }
    //3搜索会员
    //public Member inquire(String number,String password){
    //   Iterator<Member> ir=set.iterator();
    //    while (ir.hasNext()){
    //       Member ma1=ir.next();
    //        if (ma1.getNumber().equals(number)&&ma1.getPassword().equals(password)){
    //            System.out.println(ma1);
    //           return ma1;
    //        }
    //    }
    //    return null;
    //}
    //4积分累计
    public boolean accumulation(String number,int integrals,String password){
        Iterator<Member> ir=set.iterator();
        while (ir.hasNext()){
            Member me1=ir.next();
            if (me1.getNumber().equals(number)&&me1.getPassword().equals(password)){
                me1.setIntegral(me1.getIntegral()+integrals);
                return true;
            }
        }
        return false;
    }
    //5修改密码
    public void modiflcation(String number,String password,String passwords){
        Iterator<Member> it=set.iterator();
        while (it.hasNext()){
            Member me1=it.next();
            if (me1.getNumber().equals(number)&&me1.getPassword().equals(password)&&!(me1.getPassword().equals(passwords))){
                me1.setPassword(passwords);
                return;
            }
        }
        System.out.println("修改失败！");

    }
    //6积分兑换
    public void conversion(String number,String password,int integrals){
        Iterator<Member> it=set.iterator();
        while (it.hasNext()){
            Member me1=it.next();
            if (me1.getNumber().equals(number)&&me1.getPassword().equals(password)){
                if (me1.getIntegral()>=integrals) {
                    me1.setIntegral(me1.getIntegral() - integrals);
                    return;
                }
                System.out.println("积分不够！");
                return;
            }
        }
        System.out.println("操作有误！");
        return;
    }

}
